package cn.fornever.tools.db;

import cn.fornever.tools.db.assist.entity.ColumnInfo;
import cn.fornever.tools.db.assist.entity.ForeignKeyInfo;
import cn.fornever.tools.db.assist.entity.PrimaryKeyInfo;
import cn.fornever.tools.db.assist.entity.TableInfo;
import org.junit.Assert;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.List;

/**
 * 说明:
 *
 * @author ZhangAY
 * @date 2017-07-27
 */
public class DBUtilTestBase {

    public DatabaseMetaData getDatabaseMetaData(Connection connection) {
        DatabaseMetaData metaData = DBUtil.getDatabaseMetaData(connection);
        try {
            System.out.println(metaData.getDatabaseMajorVersion());
            System.out.println(metaData.getDatabaseMinorVersion());
            System.out.println(metaData.getDatabaseProductName());
            System.out.println(metaData.getDatabaseProductVersion());
            System.out.println(metaData.getMaxColumnsInSelect());
            System.out.println(metaData.getMaxColumnNameLength());
            System.out.println(metaData.getMaxConnections());
            System.out.println(metaData.getSearchStringEscape());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Assert.assertTrue(metaData != null);
        return metaData;
    }

    //http://www.cnblogs.com/lbangel/p/3487796.html
    public List<TableInfo> getTableInfo(Connection connection, String catalog, String schemaPattern,
                                        String tableNamePattern, String[] columnNamePattern) {
        List<TableInfo> list = DBUtil.getTableInfo(connection, catalog, schemaPattern, tableNamePattern, columnNamePattern);
        if (list != null) {
            for (TableInfo info : list) {
                System.out.println(info.toString());
            }
        }
        return list;
    }

    public List<ColumnInfo> getColumnInfo(Connection connection, String catalog, String schemaPattern,
                                          String tableNamePattern, String columnNamePattern) {
        List<ColumnInfo> list = DBUtil.getColoumInfo(connection, catalog, schemaPattern, tableNamePattern, columnNamePattern);
        if (list != null) {
            for (ColumnInfo info : list) {
                System.out.println(info.toString());
            }
        }
        return list;
    }

    public List<PrimaryKeyInfo> getPrimaryKeyInfo(Connection connection, String catalog, String schemaPattern, String tableNamePattern) {
        List<PrimaryKeyInfo> list = DBUtil.getPrimaryKeyInfo(connection, catalog, schemaPattern, tableNamePattern);
        if (list != null) {
            for (PrimaryKeyInfo info : list) {
                System.out.println(info.toString());
            }
        }
        return list;
    }

    public List<ForeignKeyInfo> getForeignKeyInfo(Connection connection, String catalog, String schemaPattern, String tableNamePattern) {
        List<ForeignKeyInfo> list = DBUtil.getForeignKeyInfo(connection, catalog, schemaPattern, tableNamePattern);
        if (list != null) {
            for (ForeignKeyInfo info : list) {
                System.out.println(info.toString());
            }
        }
        return list;
    }
}
